package com.fz.us.admin.base.bean.search.page;

import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.projection.Projection;
import com.fz.us.admin.base.bean.search.projection.ProjectionHelp;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - PagePaging
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-13 。
 * ============================================================================
 */
public class PagePaging extends PageList {

    public static final Integer DEFAULT_PAGE_NUMBER = 1;// 默认当前页
    public static final Integer DEFAULT_PAGE_SIZE = 20;// 默认每页记录数
    public static final Integer MAX_PAGE_SIZE = 500;// 最大每页记录数

    private final Integer pageNumber;
    private final Integer pageSize;

    private PagePaging(SearchEnum.ResultType resultType, Projection projection, Integer pageNumber, Integer pageSize) {
        super(resultType, projection);
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public static PagePaging simplePage() {
        return newBuilder().setResultType(SearchEnum.ResultType.list).setProjection(ProjectionHelp.none()).setPageNumber(DEFAULT_PAGE_NUMBER).setPageSize(DEFAULT_PAGE_SIZE).build();
    }

    public static PagePaging page(Integer pageNumber, Integer pageSize) {
        return newBuilder().setResultType(SearchEnum.ResultType.list).setProjection(ProjectionHelp.none()).setPageNumber(pageNumber).setPageSize((pageSize > MAX_PAGE_SIZE) ? MAX_PAGE_SIZE : pageSize).build();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder extends PageList.Builder {

        private Integer pageNumber;
        private Integer pageSize;

        @Override
        protected Builder getThis() {
            return this;
        }

        @Override
        public Builder setResultType(SearchEnum.ResultType resultType) {
            this.resultType = resultType;
            return this;
        }

        @Override
        public Builder setProjection(Projection projection) {
            this.projection = projection;
            return this;
        }

        public Builder setPageNumber(Integer pageNumber) {
            this.pageNumber = pageNumber;
            return this;
        }

        public Builder setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        @Override
        public PagePaging build() {
            Assert.notNull(resultType, "resultType 不能为空");
            Assert.notNull(projection, "projection 不能为空");
          //  Assert.isTrue(pageNumber >= 0, "pageNumber 必须大于等于 0");
          //  Assert.isTrue(pageSize > 0, "pageSize 必须大于 0");
            return new PagePaging(resultType, projection, pageNumber, pageSize);
        }

    }

}
